# docker安装
if command -v docker
then echo "docker 已安装"
else apt-get remove docker docker-engine docker.io containerd runc -y \
  && apt-get update \
  && apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common \
  && curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add - \
  && add-apt-repository \
   "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \
  $(lsb_release -cs) \
  stable" \
  && apt-get update \
  && apt-get install -y docker-ce docker-ce-cli containerd.io
fi

# docker-compose安装
if command -v docker-compose
then echo "docker-compose 已安装"
else curl -L https://get.daocloud.io/docker/compose/releases/download/v2.4.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose \
  && chmod +x /usr/local/bin/docker-compose
fi

# wget安装
if command -v wget
then echo "wget 已安装"
else apt-get install wget
fi

# git安装
if command -v git
then echo "git 已安装"
else apt-get install git
fi

# 设置项目目录
read -p "请输入用户目录：" folder

# 如果目录不存在 => 创建目录
if [ -d $folder ]
# 格式化目录，防止git拉取项目时失败
then rm -rf $folder/* && rm -f .gitignore && rm -rf .git
else mkdir $folder
fi
cd $folder

# 下载部署脚本
if [ -e "docker-compose.yml" ]
then echo "docker-compose脚本已下载"
else git init \
  && git remote add origin https://gitee.com/llqcai/docker-compose-lnmp.git \
  && git fetch origin \
  && git merge origin/master \
  && chmod -R 755 $folder \
  && rm -rf .git

# 修改nginx端口
read -p "请输入nginx开放端口(默认80): " nginxPort
sed -i 's/80:80/'$nginxPort':80/g' $folder/docker-compose.yml

# 添加网站安装文件
cd $folder/nginx/www && rm -rf * && wget https://down.dpqb.com/install.php && chmod -R 777 $folder/nginx/www

# 构建自定义镜像
# docker-compose build
fi

# 启动
docker-compose up -d